package Algorithm.StackAndQueue;

import java.util.Stack;

public class ReverseStack {

    // 方法顾名思义，移除栈底元素并返回
    public static int getAndRemoveLastElement(Stack<Integer> stack) {
        int res = stack.pop();
        if (stack.isEmpty()) {
            return res;
        } else {
            int last = getAndRemoveLastElement(stack);
            stack.push(res);
            return last;
        }
    }

    public static void reverseStack(Stack<Integer> stack) {
        int last = getAndRemoveLastElement(stack);
        if (!stack.isEmpty()) {
            reverseStack(stack);
        }
        stack.push(last);
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 100; i++) {
            stack.push(i);
        }
        reverseStack(stack);
    }

}
